package com.maszy.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.service.DoorStoreService;
import com.maszy.service.RfidService;

public class RfidServlet extends HttpServlet{
	
	String view = new String(); // 页面路径
	RfidService rs=new RfidService();
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		RfidServlet servlet = new RfidServlet();
		RequestDispatcher rd = null;
		
		String method = request.getParameter("method");		
		
		
		if ("queryAll".equals(method)) {
			// 查询销售明细
			servlet.queryAll(request, response);
			view = "/background/rfid/rfidTable.jsp";
		} 
		else if ("queryAllForward".equals(method)) {
			// 进入查询页面			
			servlet.queryAllForward(request, response);
			view = "/background/rfid/rfidMain.jsp";
		}else if ("queryReportForward".equals(method)) {
			// 进入查询页面			
			
			servlet.queryReportForward(request, response);
			view = "/background/rfid/rfidReportMain.jsp";
		}else if ("queryReport".equals(method)) {
			// 进入查询页面			
			servlet.queryReport(request, response);			
			view = "/background/rfid/rfidReportTable.jsp";
		}    

		try {
			request.setAttribute("method", method);
			
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String where =request.getParameter("where");
		String time=request.getParameter("time");
		String card_no=request.getParameter("card_no");
		String DoorStore_Id=request.getParameter("DoorStore_Id");
		
		Map m=new HashMap();
		m.put("DoorStore_Id", DoorStore_Id);
		
		m.put("time", time);
		m.put("card_no", card_no);
		if("1".equals(where)){
			//where=1,查询消费和充值记录
			m.put("status", null);
		}else if("2".equals(where)){
			//where=2，查询消费记录
			m.put("status", "2");
		}else if("3".equals(where)){
			//where=3,查询充值记录
			m.put("status", "1");
		}
//		System.out.println(time+"\t"+where);
		List list=rs.queryRfid(m);
//		System.out.println(list.size());
		request.setAttribute("list", list);
		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		
		doGet(request, response);
	}
	private void queryAllForward(HttpServletRequest request,
			HttpServletResponse response) {
		DoorStoreService dss=new DoorStoreService();
		List dssList=dss.queryAll();		
		request.setAttribute("dssList",dssList);
	}	
	private void queryReportForward(HttpServletRequest request,
			HttpServletResponse response) {
		Map m=rs.queryReportTotal();
		BigDecimal fill = (BigDecimal) m.get("fill");
		BigDecimal pay = (BigDecimal) m.get("pay");
		BigDecimal left=fill.subtract(pay);
		m.put("left",left);
		request.setAttribute("m",m);
		
		
	}
	private void queryReport(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String time1=request.getParameter("time1");
		String time2=request.getParameter("time2");
		Map m=new HashMap();
		m.put("time1", time1);
		m.put("time2", time2);
		List list=rs.queryReport(m);
		request.setAttribute("list", list);
		
	}
}
